package pos.data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import pos.domain.Client;
import pos.utils.UIDGenerator;

public class JDBCClientDAO implements IClientDAO {
	private IUserDAO udao;
	private Connection conn;
	
	public JDBCClientDAO() {
		this.udao = new JDBCUserDAO();
		this.conn = ConnectionManager.getInstance().checkOut();
	}
	
	public void insertClient (Client cliente) {
		String sql = "INSERT INTO clients (oid, userID) VALUES(?, ?)";
		
		PreparedStatement stmt = null;
		try{		
			String usuarioid = UIDGenerator.getInstance().getKey();

			udao.insertUser(cliente);
			
			stmt = conn.prepareStatement(sql);			
			stmt.setString(1, usuarioid);
			stmt.setString(2, cliente.getUserID());
			stmt.setString(3, cliente.getName());
			stmt.setString(4, cliente.getSubname());
			stmt.setString(5, cliente.getAddress());
			stmt.setString(6, cliente.getZipCode());
			stmt.setString(7, cliente.getCity());
			stmt.setString(8, cliente.getProvincy());
			stmt.setString(9, cliente.getTelefone());
			stmt.setString(10, cliente.getEmail());
			stmt.setString(11, cliente.getUser());
			stmt.setString(12, cliente.getPassword());
			
			stmt.execute();
			
		} catch (SQLException e) {
			System.out.println("Message: " + e.getMessage());
			System.out.println("SQLState: " + e.getSQLState());
			System.out.println("ErrorCode: " + e.getErrorCode());
		} finally {
			try {
				if (stmt != null) {
					stmt.close();
				}
			} catch (SQLException e) {
			}
		}
	}
	
	
	public List<Client> selectClient (String userid) {
		List<Client> details = new LinkedList<Client>();
		String sql = "SELECT * FROM clients WHERE (userID = ?)";
		PreparedStatement stmt = null;
		ResultSet result = null;
		
		try {
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, userid);
			result = stmt.executeQuery();

		while (result.next()) {
				Client c = new Client();
				c.setUserID(result.getString("userID"));
				details.add(c);
			}
		} catch (SQLException e) {
			System.out.println("Message: " + e.getMessage());
			System.out.println("SQLState: " + e.getSQLState());
			System.out.println("ErrorCode: " + e.getErrorCode());
		} finally {
			try {
				if (result != null) {
					result.close();
				}
				if (stmt != null) {
					stmt.close();
				}
			} catch (SQLException e) {
			}
		}
		return details;
	}
	
	public void deleteClient (String userID) {
        String sql = "DELETE FROM Clients WHERE (userID = ?)";
        PreparedStatement stmt = null;
        
        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, userID);
            stmt.executeUpdate();
        
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }
        }
    }
}